const db = require('../model')
const Stock = db.stock
const Op = db.Sequelize.Op


// 查询库存信息
exports.findOne = async (req, res) => {
	const { medInfoId, medName, stockNum } = req.query
	console.log(medInfoId, medName, stockNum)
	const data = await Stock.findOne({
		where: { medInfoId, medName, stockNum },
	})
	if (!data) {
		return res.status(404).end()//一定要return 
	}
	return res.status(200).send({
		data,//和前端对应code
	})
}
// 新增药品库存
exports.createOne = async (req, res) => {
	const { medInfoId, medName, stockNum, pharmacyPlace, pharmacyId } = req.query
	const createData =  UsersModel.create({ medInfoId, medName, stockNum, pharmacyPlace, pharmacyId })
	createData = await createData.save();
	if (!createData) {
		return res.status(404).end()//一定要return 
	}
	return res.status(200).send({
		data,//和前端对应code
	}) 
}
//更新库存信息
exports.update = async (req, res) => {
	const { medInfoId, stockNum, pharmacyPlace, pharmacyId, stockNumnew }= req.query
	const updateData = await Stock.findOne({
		where: { medInfoId, stockNum, pharmacyPlace, pharmacyId },
	  })
      updateData.stockNum = stockNumnew
	  updateData.save();
	  if (!updateData) {
		return res.status(404).end()//一定要return 
	}
	return res.status(200).send({
		data,//和前端对应code
	}) 
}
//删除库存信息
exports.delete = async (req, res) => { 
	const { medInfoId, medName, stockNum, pharmacyPlace, pharmacyId } = req.query
	const deleteData = await Stock.findOne({
		where: { medInfoId, medName, stockNum, pharmacyPlace, pharmacyId },
	});
	await deleteData.destroy();
	if (!deleteData) {
		return res.status(404).end()//一定要return 
	}
	return res.status(200).send({
		data,//和前端对应code
	}) 
}

